package goldeneagle.com.admin.servlet;

import com.alibaba.fastjson.JSONObject;
import goldeneagle.com.admin.service.AdminManagerService;
import goldeneagle.com.entity.Product;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

@WebServlet(value = "/addProduct")
public class AddProductServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        String name = request.getParameter("name");
//        String p = request.getParameter("price");
//        Integer price = Integer.parseInt(p);
//        String description = request.getParameter("description");
//        String img = request.getParameter("img");
//        String ptype = request.getParameter("ptype");
//        String json = request.getParameter("json");
        Product product = new Product();
//        product.setName(name);
//        product.setPrice(price);
//        product.setDescription(description);
//        product.setImg(img);
//        product.setPtype(ptype);



        boolean ismultipart = ServletFileUpload.isMultipartContent(request);
        // 判断表单中是否存在multipart属性
        if (ismultipart) {
            try {
                // 创建工厂类
                DiskFileItemFactory factory;
                factory = new DiskFileItemFactory();
                // 创建解析器
                ServletFileUpload upload = new ServletFileUpload(factory);
                // 使用解析器解析解析form表单中的请求字段，并保存到items集合中
                List<FileItem> items = upload.parseRequest(request);
                Iterator<FileItem> iterator = items.iterator();
                HashMap<String, String> propertyMap = new HashMap<String, String>();
                while (iterator.hasNext()) {
                    FileItem item = iterator.next();
                    // 获取请求名称
                    String itemName = item.getFieldName();
                    System.out.println(itemName);
                    // 判断请求字段是普通文本项<true>还是文件项<false>
                    if (item.isFormField()) {
                        // System.out.println(item.getString("UTF-8"));
                        propertyMap.put(itemName, item.getString("UTF-8"));

                    } else {
                        String fileName = item.getName();
                        // System.out.println(fileName);
                        // 获取服务器端实际路径
                        if (!fileName.equals("")) {
                            String path = request.getSession().getServletContext().getRealPath("upload");
                            System.out.println(path);
                            File file = new File(path, fileName);
                            //
                            File mkdir = new File(path);
                            mkdir.mkdir();
                            item.write(file);
                            propertyMap.put(itemName, fileName);
                        }
                        //propertyMap.put(itemName, "img1.gif");
                    }
                }
                 product = new Product(propertyMap.get("name"),Integer.parseInt(propertyMap.get("price")),propertyMap.get("description"),propertyMap.get("img"),propertyMap.get("ptype"));

            } catch (FileUploadBase.SizeLimitExceededException e) {
                System.out.println("文件过大，请修改");
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        //Product product1 = JSONObject.parseObject(json,Product.class);
        System.out.println(product.getName());
        AdminManagerService adminManagerService = new AdminManagerService();
        boolean addProduct = adminManagerService.adminAddProduct(product);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request,response);
    }
}
